#ifndef _UTIL_AUTOBORDER_H_
#define _UTIL_AUTOBORDER_H_

#include "ui/sprite_bin.h"


// usage:
// 1) derive new class based on AutoBorder
// 2) setup base image in derived class' constructor
// 3) in function that creates autoborder object, call Init


class AutoBorder
{
public:
	AutoBorder();
	virtual ~AutoBorder();
	
	virtual void		Init(unsigned int Width, unsigned int Height);

	void				RefreshBorders();
	virtual void		RefreshBorder(int x, int y);
	virtual void		Tick(float dt);
	virtual void		Render(const math::Vec2& Offset = math::Vec2(0.0f, 0.0f), COLOR c = -1);

	void				SetBaseImage(const std::string& BaseImage)	{ m_BaseImage = BaseImage; RefreshImages(); }

	virtual bool		SortSprites() const { return false; }
	virtual bool		BlendSprites() const { return false; }
	virtual bool		ResizeSprites() const { return true; }
	virtual bool		CenterSprites() const { return false; }
	virtual math::Vec2	GetSpriteOffset() const { return math::Vec2(0.0f, 0.0f); }

protected:
	virtual void		RefreshImages();
	unsigned char		GetBorderExtents(int x, int y);

	virtual bool		IsBorderSpace(int x, int y) = 0; // can draw a border here?
	virtual bool		IsOpenSpace(int x, int y) = 0; // should wall consider this space "open"?
	virtual math::Vec2	GetTileSize() = 0; // what is the tile size?
	virtual void		MapScreenPos(int x, int y, math::Vec2& Pos) = 0; // map x-y coordinates to vec2 pos

	ui::SpriteBin		m_SpriteBin;
	std::string			m_BaseImage;

	enum BORDER_TYPE {
		BT_NONE = 0,
		BT_WALL_L,			// standard left/right/top/bottom single wall
		BT_WALL_R,
		BT_WALL_T,
		BT_WALL_B,
		BT_WALL_CORNER_TL,	// L-shaped corners
		BT_WALL_CORNER_TR,
		BT_WALL_CORNER_BL,
		BT_WALL_CORNER_BR,
		BT_WALL_U_L,		// U-shaped borders, with openings at left/right/top/bottom
		BT_WALL_U_R,
		BT_WALL_U_T,
		BT_WALL_U_B,
		BT_WALL_EDGE_TL,	// edge of border
		BT_WALL_EDGE_TR,
		BT_WALL_EDGE_BL,
		BT_WALL_EDGE_BR,
		BT_WALL_ENCLOSED,	// fully enclosed on all four sides

		BORDER_COUNT,
	};
	ui::UISprite		m_SpriteA[BORDER_COUNT];
	virtual std::string	MapTypeToString(const BORDER_TYPE& type);	// override this to use your own naming scheme

	static const int	MAX_BORDER_LAYERS = 4;
	struct BORDER_DATA {
		unsigned char	Extents;
		BORDER_TYPE		TypeA[MAX_BORDER_LAYERS];
	};
	BORDER_DATA*		m_pData;
	unsigned int		m_Width;
	unsigned int		m_Height;

private:
	void				ParseData(BORDER_DATA* pData);
};

#endif //_UTIL_AUTOBORDER_H_
